package web.mb;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

import metier.entities.ImageSite;
import metier.sess.IImageSiteLocal;



@ManagedBean
@RequestScoped
public class ImageSiteMB {

	
	@EJB
	private IImageSiteLocal metier;
	
	private Integer Id;
	private ImageSite image=new ImageSite();
	private String destination = "\\C:\\workspace\\CargaisonWeb\\WebContent\\Admin\\Manage_ImageSite\\images\\";

	 private UploadedFile file;
	 
	    public UploadedFile getFile() {
	        return file;
	    }
	 
	    public void setFile(UploadedFile file) {
	        this.file = file;
	    }
	
	public String getDestination() {
		return destination;
	}

	public void setDestination(String destination) {
		this.destination = destination;
	}

	public Integer getId() {
		return Id;
	}
	
	public void setId(Integer id) {
		Id = id;
	}

	public ImageSite getImage() {
		return image;
	}

	public void setImage(ImageSite image) {
		this.image = image;
	}

	public String Ajouter()
	{
		image.setUrlImage("./images/"+file.getFileName());
		metier.addImageSite(image);
		upload();
		return "ManageImageSite.xhtml";
		
	}
	/////////////////////////
	
	

	public void copyFile(String fileName, InputStream in) {
		try {

			// write the inputStream to a FileOutputStream
			OutputStream out = new FileOutputStream(new File(destination
					+ fileName));

			int read = 0;
			byte[] bytes = new byte[1024];

			while ((read = in.read(bytes)) != -1) {
				out.write(bytes, 0, read);
			}

			in.close();
			out.flush();
			out.close();

			System.out.println("New file created!");
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
	}
	  public void upload() {
	        if(file != null) {
	        	FacesMessage msg = new FacesMessage("Success! ",file.getFileName()+ " is uploaded.");
	    		FacesContext.getCurrentInstance().addMessage(null, msg);
	    		// Do what you want with the file
	    		try {

	    			copyFile(file.getFileName(), file.getInputstream());

	    		} catch (IOException e) {
	    			e.printStackTrace();
	    		}
	        }
	    }
	//////////////////////////////////////////
	
	public String edit(Integer Id)
	{
		this.Id=Id;
		image=metier.getImageSite(Id);
	   
		return "EditImageSite.xhtml";
		
	}
	
	public String Modifier()
	{
		ImageSite d=metier.getImageSite(Id);
		d=image;
		
		metier.updateImageSite(d);
		return "ManageImageSite.xhtml";
	}

	public String Supprimer(Integer Id)
	{
		
		metier.deleteImageSite(Id);
		return "succes";
		
	}
	
	
	public List<ImageSite> getLisImageSite()
	{
		return metier.getAllImageSite();
	}


	
	
}
